// 获取列表数据
function datalist(page,pageCount,name,type,state,remarks){

    // 清空一下列表
    $("#information_data").empty();//清空节点
    sleep(200)
    return $.ajax({
        type: 'POST',
        url: "/information/getInformationSelectList",
        dataType:"json",
        data:{
            page: page,
            pageNumber: pageCount,
            name: name,
            type: type,
            state: state,
            remarks: remarks,
        },
        success: function (message) {
            $("#information_data").append(analysis(message))
            $("#name").val(name) // 备注
            // $("#type").val(type)// 状态     .find("option[text='请选择性别']").val(2).text('女');
                //$("#state").val(state)// 级别
            $("").find("option[text='请选择性别']").val(2).text('女');
            $("#remarks").val(remarks) // 名字
            var page = {"page":message.data.page,"pageCount":message.data.pageCount,"pageNumber":message.data.pageNumber}
            return page
        }
    })
}

layui.use(['laypage'], function(){
    var datapage = {
        "page": 1,
        "pageNumber": 20,

    }
    $.ajax({
        type: 'POST',
        url: "/information/getInformationSelectList",
        dataType:"json",
        async: false,
        data:{
            page: datapage.page,
            pageNumber: datapage.pageNumber,
            name: '',
            type: '',
            state: '',
            remarks: '',
        },
        success: function (message) {
            datapage.dataCount = message.data.dataCount
        }
    })
    var laypage = layui.laypage
    //总页数低于页码总数
    laypage.render({
        limit:datapage.pageNumber,
        jump: function(obj){
            //obj包含了当前分页的所有参数，比如：
            // console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
            // console.log(obj.limit); //得到每页显示的条数
            // console.log(datapage)
            datalist(obj.curr, obj.limit,'','','','')
        },
        layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'],
        limits:[20,40,60,80,100],
        elem: 'page_all',
        count: datapage.dataCount //数据总数
    });
})


// name 名字 type 级别   state 状态	remarks备注
function getInputSelect() {
    var name = $("#name").val(); // 用户名
    var type = $("#type").val();
    var state = $("#state").val();
    var remarks = $("#remarks").val();
    if(isEmpty(name) && isEmpty(type) && isEmpty(state) && isEmpty(remarks)){
        layer.msg('请输入搜索数据！');
    }else {
        // 按正常的走
        $.ajax({
            type: 'POST',
            url: "/information/getInformationSelectList",
            dataType:"json",
            data:{
                page:1,
                pageNumber:20,
                name:name,
                type:type,
                state:state,
                remarks:remarks,
            },
            success: function (message) {
                if(message.code==0){
                    //window.location.href="./information.html?name=" + name + "&type=" + type + "&state=" + state + "&remarks=" + remarks
                    // layer.msg('增加备注成功！');
                    $("#information_data").empty();
                    $("#information_data").append(analysis(message))
                    // 重新铺一下页码
                    pageRestructure("page_all",message.data.dataCount,20,name,type,state,remarks)
                }else {
                    layer.msg('搜索为空请换其他条件！');
                }
            },error: function (message) {
                layer.msg('搜索失败请稍后重试！');
            }
        })
    }
}



// 删除
function items_delect(id) {
    layer.msg('确认删除？ 删除无法撤销！', {
        time:0,
        btn: ['取消','确定'],
        btnAlign: 'c',
        yes: function(index, layero){
            //按钮【按钮一】的回调
            console.log("取消按钮")
            layer.closeAll()
        }, btn2: function(index, layero){
            //按钮【按钮二】的回调
            $.ajax({
                type: 'POST',
                url: "/information/delectInformation",
                dataType:"json",
                data:{
                    id: id
                },
                success: function (message) {
                    console.log(message)
                    if(message.code==0){
                        layer.msg('成功删除');
                    }else {
                        layer.msg('删除失败，请稍后重试！');
                    }
                },error: function (message) {
                    layer.msg('删除失败，请稍后重试！');
                }
            })
            //return false 开启该代码可禁止点击该按钮关闭
            console.log("确定按钮")
            layer.closeAll()

            //刷新列表
            sleep(200)
            datalist(1,20);
        }
    });
}

// 增加信息
function addInformAtion() {
    layer.open({
        type: 2,
        area: ['1100px', '600px'],
        fixed: false, //不固定
        maxmin: true,
        title: '增加信息/线索',
        content: 'information-items-add.html'
    });
}

// 增加备注详情
function remarksDetails(id) {
    // console.log(id)
    //默认prompt
    layer.prompt({
        formType: 2,
        title: '请输入备注内容',
        area: ['24rem', '15rem'] //自定义文本域宽高
    },function(val, index,elem){
        // console.log(val)
        $(".layui-layer-btn0").attr({"disabled":"disabled"});
        $.ajax({
            type: 'POST',
            url: "/remarks/addGgRemarks",
            dataType:"json",
            data:{
                count: val,
                token: id,
                type:1
            },
            success: function (message) {
                if(message.code==0){
                    parent.layer.msg('增加备注成功！');
                }else {
                    parent.layer.msg('增加备注失败请重试！');
                }

            },error: function (message) {
                parent.layer.msg('增加备注失败请重试！');
            }
        })
        $(".layui-layer-btn0").removeAttr("disabled");//将按钮可用
        parent.layer.close(index);
        sleep(200)
        datalist(1,20,'','','','');
    });
}


// 删除信息
function delectAll() {
    var items_id = getInput()
    if(items_id.length==0){
        layer.msg('请选择要删除的数据！');
    }else {
        $.ajax({
            type: 'POST',
            url: "/information/delectInformationAll",
            dataType:"json",
            data:{
                //id:items_id[i],
                listid: JSON.stringify( items_id )
            },
            success: function (message) {
                console.log(message)
                if(message.code==0){
                    layer.msg('成功：'+message.data.yes+'个，找不到：'+message.data.not+'个，失败：'+message.data.error+'个');
                    sleep(200)
                    datalist(1,20,'','','','');
                }else {
                    layer.msg('删除失败，请稍后重试！');
                }
            },error: function (message) {
                layer.msg('删除失败，请稍后重试！');
            }
        })
    }
}

function getInput() {
    var obj = document.getElementsByName("ggIId");
    var check_arr = [];
    for (var i = 0; i < obj.length; i++) {
        if (obj[i].checked)
            check_arr.push(obj[i].value);
    }
    return check_arr
}

// 点击行获取行数据
function getTableTr(id) {
    // console.log("点击行"+id)
    //iframe层-父子操作
    layer.open({
        type: 2,
        area: ['700px', '450px'],
        fixed: false, //不固定
        maxmin: true,
        title: '备注详情',
        closeBtn: 0,
        shadeClose: true,
        skin: 'layui-layer-rim', //加上边框
        content: 'remarks-details.html?id='+id
    });
}

// 加入商机
function addbusiness(id) {
    if(!isEmpty(id)){
        $.ajax({
            type: 'POST',
            url: "/business/addbusiness",
            dataType:"json",
            data:{
                id:id,
            },
            success: function (message) {
                if(message.code==0){
                    location.reload()
                }else {
                    layer.msg('加入商机失败，请稍后重试！');
                }
            },error: function (message) {
                layer.msg('搜索失败，请稍后重试！');
            }
        })
    }else {
        layer.msg('请选择要加入商机的数据！');
    }
}

// 多个加入商机
function addinformationAll() {
    var items_id = getInput()

    if(items_id.length==0){
        layer.msg('请选择要加入商机的数据！');
    }else {
        $.ajax({
            type: 'POST',
            url: "/information/removebusinessAll",
            dataType:"json",
            data:{
                //id:items_id[i],
                listid: JSON.stringify( items_id )
            },
            success: function (message) {
                console.log(message)
                if(message.code==0){
                    layer.msg('成功：'+message.data.yes+'个，找不到：'+message.data.not+'个，失败：'+message.data.error+'个');
                    sleep(200)
                    datalist(1,20,'','','','');
                }else {
                    layer.msg('删除失败，请稍后重试！');
                }
            },error: function (message) {
                layer.msg('删除失败，请稍后重试！');
            }
        })
    }
}

// 解析列表data
function analysis(message) {
    trlist = []
    for (i = 0;i < message.data.data.length;i++ ){
        var dataInformation = message.data.data[i];
        var remarks = dataInformation.ggIRemarks;
        var serialNumber = i+1
        if(isEmpty(remarks)){
            remarks = ""
        }else if(remarks.length>35){
            remarks = remarks.substring(0,35)+'......'
        }
        // 信息唯一标识符
        ggItemsToken = "'"+dataInformation.ggItemsToken+"'"
        var tr =
            '<tr>'+
            '<td><input type="checkbox" name="ggIId" value="'+dataInformation.ggIId+'"> </td>'+
            '<td> '+serialNumber+'</td>'+
            '<td onclick="getTableTr('+ggItemsToken+')">'+dataInformation.ggIName+'</td>'+
            '<td onclick="getTableTr('+ggItemsToken+')">'+dataInformation.ggIProvince+'/'+dataInformation.ggICity+'/'+ dataInformation.ggIAreaid+'/'+ dataInformation.ggIDetailed + '</td>'+
            '<td onclick="getTableTr('+ggItemsToken+')">'+ dataInformation.ggIHandle+ '</td>'+
            '<td onclick="getTableTr('+ggItemsToken+')"><button type="button" class="layui-btn layui-btn-warm-phone">'+dataInformation.ggITelephone+'</button></td>'+
            '<td onclick="getTableTr('+dataInformation.ggIId+')"><button type="button" class="layui-btn layui-btn-warml">'+dataInformation.ggILevel+'</button></td>'+
            '<td onclick="getTableTr('+dataInformation.ggIId+')"><button type="button" class="layui-btn layui-btn-warm">'+dataInformation.ggIState+'</button></td>'+
            '<td onclick="getTableTr('+ggItemsToken+')">'+remarks+'</button></td>'+
            '<td><a href="./information-items.html?id='+dataInformation.ggItemsToken+'">查看信息</a>  <a href="#" onclick="items_delect('+dataInformation.ggIId+')">删除信息</a href="">  <a href="#" onclick="informationItemsUpdate('+ggItemsToken+')">修改信息</a><br/> <a href="#" onclick="remarksDetails('+ggItemsToken+')">增加备注</a> <a href="#" onclick="addbusiness('+dataInformation.ggIId+')">移入商机</a> <a href="#" onclick="">移入签约</a></td>'+
            '</tr>'
        trlist.push(tr)
    }
    return trlist
}


// 修改信息
function informationItemsUpdate(ggItemsToken) {
    console.log("a")
    layer.open({
        type: 2,
        area: ["1100px", "600px"],
        fixed: false, //不固定
        maxmin: true,
        title: '修改信息',
        content: 'information-items-update.html?id='+ggItemsToken
    });
}
